<?php
class Category extends AppModel{
    public $useTable  = 'category';
    public $lang_code = 'vn';
    
    public function get_category_id_by_code($category_code = null)
    {
        $sql = array('conditions' => array('category_code =' => $category_code, 'status =' => 1), 'limit' => 1);
        $categorys = $this->find('all', $sql);
        if(!empty($categorys))
            return $categorys[0]['Category']['category_id'];
        return 0;
    }
    
    public function get_info_category_by_id($category_id = null)
    {
        $lang_code = $this->lang_code;
        $sql = "SELECT category.*, category_desc.*, link.link
        FROM category_desc, category
        LEFT JOIN link ON category.category_id = link.post_id AND link.type = 'category' AND link.lang_code = '$lang_code'
        WHERE category.category_id = category_desc.category_id AND category.category_id = $category_id AND category_desc.lang_code = '$lang_code'";
        
        $category = $this->query($sql);
        if(!empty($category))
            return $category[0];
        else
            return array();
    }
    
    public function list_category($parent_id = 0, $data = NULL) {
        if (!$data) {
            $data = array();
        }
        $lang_code = $this->lang_code;
        $sql = "SELECT category.*, image.name, category_desc.title, link.link
				FROM category_desc, category
                LEFT JOIN image ON category.category_id = image.post_id AND image.type = 'category' AND image.`status` = 1
                LEFT JOIN link ON category.category_id = link.post_id AND link.type = 'category' AND link.lang_code = '$lang_code'
                WHERE 1=1 AND category.parent_id = $parent_id AND category.`status` = 1 AND category.`category_id` = category_desc.`category_id` 
                AND category_desc.`lang_code` = '$lang_code'
				ORDER BY category.sort_order";
        $row = $this->query($sql);
        if ($row)
            foreach ($row as $key => $value) {
                $data[$key] = $value;
                $data[$key]['sub_category'] = $this->list_category($value['category']['category_id']);
            }
        return $data;
    }

    public function get_category_by_code($category_code = null)
    {
        if($category_code == null)
            return array();
            
        $category_id = $this->get_category_id_by_code($category_code);
        if($category_id)
        {
            return $this->list_category($category_id);
        }
        else
        {
            return array();
        }
    }
    
    
    
    public function get_all_id_category()
    {
        $sql = "SELECT category.category_id, category.parent_id FROM category WHERE category.`status` = 1";
		$category = $this->query($sql);
        $list_category = array();
        foreach($category as $value)
        {
            //$list_category[$value['category']['category_id']] = $value['category']['parent_id'];
            $list_category[] = $value['category'];
        }
        return $list_category;
    }
    
    function filter_category($arrData, $parent = 0, $level = 0, &$result = array()) {
        if (count($arrData) > 0) {
            foreach ($arrData as $key => $val) {
                if ($parent == $val['parent_id']) {
                    $val['level'] = $level;
                    $result[] = $val;
                    $_parent = $val['category_id'];
                    unset($arrData[$key]);
                    $this->filter_category($arrData, $_parent, $level + 1, $result);
                }
            }
        }
        return $result;
    }
    
    function filter_category_by_id($data = array(), $parent_id = null)
    {
        $result = array();
        foreach($data as $value)
        {
            $value['category_id'];
            if($value['category_id'] == $parent_id)
            {
                $result[] = $parent_id;
            }
            else
            {
                $p_id = $value['parent_id'];
                if(in_array($p_id, $result))
                    $result[] = $value['category_id'];
            }
        }
        return $result;
    }
    
    function get_parent_cate_by_child_cate($level = null, $category_id = null)
    {
        $list_category = $this->get_all_id_category();
        $filter_category = $this->filter_category($list_category);
        $temp = '';
        foreach($filter_category as $cate)
        {
            if($cate['level'] == $level)
                $temp = $cate['category_id'];
            if($cate['category_id'] == $category_id)
                return $temp;
        }
        return 0;
    }
}